package com.xjmall.controller.background;

import com.xjmall.common.Const;
import com.xjmall.common.ServerResult;
import com.xjmall.exception.AuthorityException;
import com.xjmall.exception.NotLoginException;
import com.xjmall.pojo.Banner;
import com.xjmall.pojo.User;
import com.xjmall.service.IBannerService;
import com.xjmall.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 轮播图管理Controller
 * Created by An on 2017/8/26.
 */
@Controller
@RequestMapping("/manage/banner")
public class BannerController {

    @Autowired
    private IBannerService iBannerService;
    @Autowired
    private IFileService iFileService;

    /**
     * 添加轮播
     * @param session 当前会话
     * @param banner 待添加的轮播信息
     * @return ServerResult
     */
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult addBanner(HttpSession session, Banner banner) {
        // 获取当前登录用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iBannerService.addBanner(banner);
    }

    /**
     * 修改轮播
     * @param session 当前会话
     * @param banner 待修改的轮播信息
     * @return ServerResult
     */
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult updateBanner(HttpSession session, Banner banner) {
        // 获取当前登录用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iBannerService.updateBanner(banner);
    }

    /**
     * 根据轮播id获取轮播信息
     * @param session 当前会话
     * @param id 轮播id
     * @return ServerResult
     */
    @RequestMapping(value = "get.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult<Banner> getBannerById(HttpSession session, Integer id) {
        // 获取当前登录用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iBannerService.getBannerById(id);
    }

    /**
     * 获取轮播列表
     * @param session 当前会话
     * @return ServerResult
     */
    @RequestMapping(value = "list.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult<List<Banner>> bannerList(HttpSession session) {
        // 获取当前登录用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iBannerService.getBannerList();
    }

    /**
     * 根据id删除轮播
     * @param session 当前会话
     * @param id 轮播id
     * @return ServerResult
     */
    @RequestMapping(value = "delete.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult deleteBannerById(HttpSession session, Integer id) {
        // 获取当前登录用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iBannerService.deleteBannerById(id);
    }
}
